jQuery('body').ready(init);
function init(){
  pie.init();
}
var pie = {
  id:'pie',
  serverBase:'/pie/web/app_dev.php',
  uploadBase:'/pie/app/data',
  height:null,
  width:null,
  curPageId:null,
  user:{
    exist:false,
    info:null,
    data:null,
    expectations:null,
    questions:null,
    questionStatus:null,   //0代表还未回答，1代表已经回答
    seeLoveTest:null,
    seeLifeTest:null,
    personalityTest:null,
    communicationTest:null,
    matchInfo:null,
    init:function(){
      this.getInfo();
      this.getMate();
      this.getQuestions();
      this.getExpectaions();
      this.getSeeLoveTest();
      this.getSeeLifeTest();
      this.getPersonalityTest();
      this.getCommunicationTest();
      /*this.initMatch();*/
    },
    initMatch:function(){
      var userData = {
        'token':pie.user.data.access_token,
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/users/'+pie.user.data.puid+'/init.json',
        type:'patch',
        data:userData,
        dataType:'json',
        success:function(reply){
          if(reply.status == 'error'){
            alert('请完善信息!')
          }
        },
        error:function(err){
          alert('获取用户信息出错:'+err);
        },
      })
    },
    getInfo:function(){
      var userData = {
        '_format':'json',
        'token':pie.user.data.access_token,
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/users/'+pie.user.data.puid,
        type:'get',
        data:userData,
        dataType:'json',
        success:function(reply){
          if(reply.status == 'error'){
            alert('错误码'+reply.code+',请报告管理员');
            return;
          }
          pie.user.info = reply.data;
          if(pie.user.info.sex == true){
            pie.user.info.sex = 1;
          }
          else{
            pie.user.info.sex = 0;
          }
          pie.page.userHomePage.init();
        },
        error:function(err){
          alert('获取用户信息出错:'+err);
        },
      })
    },
    getMate:function(){
      var userData = {
        '_format':'json',
        'token':pie.user.data.access_token,
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/apps/pie_match/matches',
        type:'get',
        data:userData,
        dataType:'json',
        success:function(reply){
          if(reply.status != 'success'){
            alert('获取配对信息出错')
            return;
          }
          if(reply.data[0]){
            pie.user.matchInfo = reply.data[0];
            pie.mate.info = reply.data[0].target_info;
            pie.page.mateHomePage.init();
          }
          else{
            jQuery('#mateHead').click(function(){
              alert('今日尚无配对')
            })
          }
        },
        error:function(err){
          alert('error:'+err);
        }
      })
    },
    getExpectaions:function(){
      var userData = {
        '_format':'json',
        'token':pie.user.data.access_token,
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/apps/pie_match/expectations',
        type:'get',
        data:userData,
        dataType:'json',
        success:function(reply){
          pie.user.expectations = reply.data;
          pie.page.userDetailPage.expectation.init();
        },
        error:function(err){
          alert('获取用户信息出错:'+err);
        },
      })
    },
    getQuestions:function(){
      var userData = {
        'token':pie.user.data.access_token,
        'number':100//目前的上限是50
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/apps/pie_match/qa/public/random/questions.json',
        type:'get',
        data:userData,
        dataType:'json',
        success:function(reply){
          if(reply.status != 'success'){
            alert('获取用户答题信息错误')
            return;
          }
          pie.user.questions = reply.data;
          pie.user.questionStatus = [];
          for(var key in reply.data){
            pie.user.questionStatus.push(0);
          }
          pie.page.userQAPage.init();
        },
        error:function(err){
          alert('获取用户信息出错:'+err);
        },
      })
    },
    getSeeLoveTest:function(){
      var moduleCode = 'see_love';
      var userData = {
        'token':pie.user.data.access_token,
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/apps/pie_test/modules/'+moduleCode
            +'/questions.json',
        type:'get',
        data:userData,
        dataType:'json',
        success:function(reply){
          if(reply.status == 'error'){
            alert('错误码'+reply.code+',请报告管理员');
            return;
          }
          pie.user.seeLoveTest = reply.data;
          pie.page.userSeeLoveTestPage.init();
        },
        error:function(err){
          alert('获取用户信息出错:'+err);
        },
      })
    },
    getSeeLifeTest:function(){
      var moduleCode = 'see_life';
      var userData = {
        'token':pie.user.data.access_token,
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/apps/pie_test/modules/'+moduleCode
            +'/questions.json',
        type:'get',
        data:userData,
        dataType:'json',
        success:function(reply){
          if(reply.status == 'error'){
            alert('错误码'+reply.code+',请报告管理员');
            return;
          }
          pie.user.seeLifeTest = reply.data;
          pie.page.userSeeLifeTestPage.init();
        },
        error:function(err){
          alert('获取用户信息出错:'+err);
        },
      })
    },
    getPersonalityTest:function(){
      var moduleCode = 'personality';
      var userData = {
        'token':pie.user.data.access_token,
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/apps/pie_test/modules/'+moduleCode
            +'/questions.json',
        type:'get',
        data:userData,
        dataType:'json',
        success:function(reply){
          if(reply.status == 'error'){
            alert('错误码'+reply.code+',请报告管理员');
            return;
          }
          pie.user.personalityTest = reply.data;
          pie.page.userPersonalityTestPage.init();
        },
        error:function(err){
          alert('获取用户信息出错:'+err);
        },
      })
    },
    getCommunicationTest:function(){
      var moduleCode = 'communication';
      var userData = {
        'token':pie.user.data.access_token,
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/apps/pie_test/modules/'+moduleCode
            +'/questions.json',
        type:'get',
        data:userData,
        dataType:'json',
        success:function(reply){
          if(reply.status == 'error'){
            alert('错误码'+reply.code+',请报告管理员');
            return;
          }
          pie.user.communicationTest = reply.data;
          pie.page.userCommunicationTestPage.init();
        },
        error:function(err){
          alert('获取用户信息出错:'+err);
        },
      })
    },
    logout:function(){
      delete window.localStorage.pieUserAccount;
      delete window.localStorage.pieUserPassword;
      window.location.reload();
    },
  },
  mate:{
    exist:false,
    info:null,
    data:null,
    expectations:null,
    questions:null,
    questionStatus:null,   //0代表还未回答，1代表已经回答
    seeLoveTest:null,
    seeLifeTest:null,
    personalityTest:null,
    communicationTest:null,
    init:function(){
    },
    like:function(){
      var userData = {
        'token':pie.user.data.access_token,
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/apps/pie_match/matches/'+this.mid+'/like.json',
        type:'patch',
        data:userData,
        dataType:'json',
        success:function(reply){
          for(var key in reply){
            alert(key+'='+reply[key])
          }
        },
        error:function(err){
          alert('error:'+err)
        }
      })
    },
    pass:function(){
      var userData = {
        'token':pie.user.data.access_token,
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/apps/pie_match/matches/'+this.mid+'/pass.json',
        type:'patch',
        data:userData,
        dataType:'json',
        success:function(reply){
          for(var key in reply){
            alert(key+'='+reply[key])
          }
       },
        error:function(err){
          alert('error:'+err)
        }
      })
    },
    getInfo:function(){
      var userData = {
        '_format':'json',
        'token':pie.user.data.access_token,
      }
      jQuery.ajax({
        url:pie.serverBase+'/api/rest/users/{random}',
        type:'get',
        data:userData,
        dataType:'json',
        success:function(reply){
          if(reply.status == 'error'){
            alert('错误码'+reply.code+',请报告管理员');
            return;
          }
          pie.user.info = reply.data;
          if(pie.user.info.sex == true){
            pie.user.info.sex = 1;
          }
          else{
            pie.user.info.sex = 0;
          }
          userHead.init();
          userWindow.init();
          userWindow.details.info.init();
          userWindow.details.describe.init();
        },
        error:function(err){
          alert('获取用户信息出错:'+err);
        },
      })
    },
    getExpectaions:function(){
    },
    getQuestions:function(){
    },
  },
  page:{
    indexPage:{
      id:'indexPage',
      resize:function(){
        jQuery('#'+this.id).css('height',pie.height+'px');
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    pieLoginPage:{
      id:'pieLoginPage',
      resize:function(){

      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){
        jQuery('#pieLoginAccountInput').focus();
      },
      submit:function(){
        var account = jQuery('#pieLoginAccountInput').val();
        if(account == ""){
          alert('帐号不能为空');
          jQuery('#pieLoginAccountInput').focus();
          return
        }
        var passwordMode = /^[a-zA-Z0-9]{6,16}$/;
        var password = jQuery('#pieLoginPasswordInput').val()
        if(!passwordMode.test(password)){
          alert('帐号或密码错误');
          jQuery('#pieLoginPasswordInput').focus();
          return;
        }
        var loginData = {
          '_format':'json',
          'app_token':'app_token',
          'userkey':account,
          'passwd':password
        }
        jQuery.ajax({
          url:pie.serverBase+'/api/auth/token/via/passwd',
          type:'post',
          data:loginData,
          dataType:'json',
          success:function(reply){
            if(reply.status == 'error'){
              switch(reply.code){
                case 1011:alert('请输入正确的帐号');break;
                case 1012:alert('请输入密码');break;
                case 1013:alert('帐号不存在');break;
                case 1014:alert('密码不正确');break;
                case 1015:alert('帐号未激活');break;
                default:alert('登录碰到未知错误:'+reply.reason);break;
              }
            }
            else{
              pie.user.exist = true;
              pie.user.data = reply.data;
              pie.user.init();
              window.localStorage.pieUserAccount = loginData.userkey;
              window.localStorage.pieUserPassword = loginData.passwd;
              pie.page.pieMatchPage.show();
            }
          },
          error:function(err){
            alert('登录碰到未知错误,你的网络连接状况不太行吧?连不上啊')
          }
        })
      }
    },
    pieRegPage:{
      id:'pieRegPage',
      resize:function(){
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){
        jQuery('#pieRegMobileInput').focus();
      },
      submit:function(){
        var mobileMode = /[0-9]{11}$/;
        var mobile = jQuery('#pieRegMobileInput').val();
        if(!mobileMode.test(mobile)){
          alert('请输入正确的11位手机号');
          jQuery('#pieRegMobileInput').focus();
          return;
        }
        var emailMode = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var email = jQuery('#pieRegEmailInput').val();
        if(!emailMode.test(email)){
          alert('请输入正确的邮箱');
          jQuery('#pieRegEmailInput').focus();
          return;
        }
        var passwordMode = /^[a-zA-Z0-9]{6,16}$/;
        var password = jQuery('#pieRegPasswordInput').val();
        if(!passwordMode.test(password)){
          alert('请输入6~16位字符的密码');
          jQuery('#pieRegPasswordInput').focus();
          return;
        }
        var passwordRepeat = jQuery('#pieRegPasswordRepeatInput').val();
        if(password != passwordRepeat){
          alert('请输入相同的密码');
          jQuery('#pieRegPasswordRepeatInput').focus();
          return;
        }
        var regData = {
          'app_token':'app_token',
          '_format':'json',
          'mobile_phone':mobile,
          'email':email,
          'passwd':password
        }
        jQuery.ajax({
          url:pie.serverBase+'/api/auth/account/register/via/mobile/phone',
          type:'post',
          data:regData,
          dataType:'json',
          success:function(reply){
            if(reply.status == 'success'){
              alert('激活码已发至您的手机,请查看');
              pie.page.pieActivatePage.show();
            }
            else{
              switch(reply.code){
                case 1081:alert('请输入正确的信息');break;
                case 1082:alert('用户名已被注册');break;
                case 1083:alert('该邮箱已被注册');break;
                case 1084:alert('该手机已被注册');break;
                default:alert('注册未知错误:'+reply.message);break;
              }
            }
          },
          error:function(err){
            alert('注册碰到未知错误，请联系管理员')
          }
        })
      },
    },
    pieActivatePage:{
      id:'pieActivatePage',
      resize:function(){
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
      submit:function(){
        var code = jQuery('#pieActivateInput').val();
        var mobile = jQuery('#pieRegMobileInput').val();
        var activateData = {
          '_format':'json',
          'app_token':'app_token',
          'mobile_phone':mobile,
          'security_code':code
        }
        jQuery.ajax({
          url:pie.serverBase+'/api/auth/account/activate/by/security/code',
          type:'post',
          data:activateData,
          dataType:'json',
          success:function(reply){
            if(reply.status == 'success'){
              var mobile = jQuery('#pieRegMobileInput').val();
              jQuery('#pieLoginAccountInput').val(mobile);
              var password = jQuery('#pieRegPasswordInput').val()
              jQuery('#pieLoginPasswordInput').val(password);
              pie.page.pieLoginPage.submit();
            }
            else{
              alert('激活码错误');
              jQuery('#pieActivateInput').val('');
            }
          },
          error:function(err){
            alert('error:'+err);
          }
        })
      },
    },
    pieMatchPage:{
      id:'pieMatchPage',
      headSize:null,
      resize:function(){
        this.headSize = pie.width/5;
        jQuery('#'+this.id+' .pageHeader').css('height',this.headSize+'px');
        var bodyHeight = pie.height-40-2*this.headSize;
        jQuery('#'+this.id+' .pageBody').css('height',bodyHeight+'px');
        jQuery('#pieMatchInfoBox').css('margin-top',this.headSize/2+10+'px');
        var countDownHeight = 57;
        jQuery('#countDown').css('margin-top',(bodyHeight-countDownHeight)/2+'px');
        jQuery('#'+this.id+' .pageFooter').css('height',this.headSize+'px');
        jQuery('#userHead').css('width',this.headSize+'px');
        jQuery('#userHead').css('height',this.headSize+'px');
        jQuery('#userHead').css('margin-top',-this.headSize/2+'px');
        jQuery('#userHead').css('margin-left',(pie.width-this.headSize)/2+'px');
        jQuery('#mateHead').css('width',this.headSize+'px');
        jQuery('#mateHead').css('height',this.headSize+'px');
        jQuery('#mateHead').css('margin-top',this.headSize/2+'px');
        jQuery('#mateHead').css('margin-left',(pie.width-this.headSize)/2+'px');
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    userHomePage:{
      id:'userHomePage',
      resize:function(){
        jQuery('#'+this.id).css('height',pie.height+'px');
        jQuery('#'+this.id+' .homeFooter').css('height',2*Math.floor(pie.width/3)+'px');
        jQuery('#'+this.id+' .homeBody').css('height',pie.height-2*Math.floor(pie.width/3)+'px')
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      init:function(){
        this.photoInit();
        if(pie.user.info.photos.length > 0){
          var smallUrl = pie.serverBase+'/asset/'+pie.user.info.photos[0].uri+'/'+pie.page.pieMatchPage.headSize+'/'+pie.page.pieMatchPage.headSize+'/outer/inline';
          jQuery('#userHead').css('background-image','url('+smallUrl+')');
          var bigUrl = pie.serverBase+'/asset/'+pie.user.info.photos[0].uri+'/'+(pie.height-2*Math.floor(pie.width/3))+'/'+(pie.height-2*Math.floor(pie.width/3))+'/outer/inline';
          jQuery('#userHomePage .homeBody').css('background-image','url('+bigUrl+')');
        }
      },
      selectPhoto:function(){
        document.getElementById('form_file').click();
      },
      uploadPhoto:function(){
        var form = document.getElementById('uploadPhotoForm');
        var action = pie.serverBase+'/api/rest/users/'+pie.user.data.puid+'/photos.json';
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function(){
          if(xhr.readyState == 4){
            if(xhr.status == 200){
              var reply = jQuery.parseJSON(xhr.responseText)
              if(reply.status == 'success'){
                /*pie.user.getInfo();*/
                alert('success');
              }
              else{
                for(var key in reply){
                  alert(key+' = ' +reply[key])
                }
              }
            }
          }
        }
        xhr.open('POST',action);
        xhr.send(new FormData(form));
      },
      photoInit:function(){
        var uploadHtml = '<div id="uploadPhoto" class="userPhoto" onclick="pie.page'
        +'.userHomePage.selectPhoto()"><form id="uploadPhotoForm" class="hidden"><input type="file"'
        +' id="form_file" name="file" onchange="pie.page.userHomePage.uploadPhoto()"><input'
        +' type="hidden" id="access_token" name="token"></form></div>';
        var photoHtml = '';
        for(var i = 0; i < pie.user.info.photos.length; i++){
          photoHtml += '<img class="userPhoto" src="'+pie.serverBase+'/asset/'+pie.user.info.photos[i].uri+'/120/120/outer/inline'+'">';
        }
        jQuery('#userPhotoArea').html(uploadHtml+photoHtml);
        jQuery('#access_token').val(pie.user.data.access_token);
      },
      getFocus:function(){},
    },
    userDetailPage:{
      id:'userDetailPage',
      curSonPage:null,
      info:{
        id:'userInfoSonPage',
        changed:false,
        init:function(){
          jQuery('#userInfoNicknameInput').val(pie.user.info.nickname);
          jQuery('#userInfoSexInput').val(pie.user.info.sex);
          jQuery('#userInfoSurnameInput').val(pie.user.info.surname);
          jQuery('#userInfoFirstnameInput').val(pie.user.info.first_name);
          jQuery('#userInfoCurrentLocationInput').val(pie.user.info.current_location.code);
          jQuery('#userInfoBirthdayInput').val(pie.user.info.birthday);
          jQuery('#userInfoHeightInput').val(pie.user.info.height);
          jQuery('#userInfoWeightInput').val(pie.user.info.weight);
          jQuery('#userInfoSchoolInput').val(pie.user.info.school);
          jQuery('#userInfoDegreeInput').val(pie.user.info.degree);
        },
        show:function(){
          userWindow.details.curSonPage.hide();
          userWindow.details.curSonPage = this;
          jQuery('#'+this.id).fadeIn();
        },
        hide:function(){
          jQuery('#'+this.id).css('display','none');
        },
        change:function(item){
          jQuery('#'+item.id).css('color','red');
          jQuery('#userDetails .headerRight span').html('提交');
          jQuery('#userDetails .headerRight').click(function(){
            userWindow.details.info.submit();
          })
          this.changed = true;
        },
        submit:function(){
          if(!this.changed){return;}
          var nickname = jQuery('#userInfoNicknameInput').val();
          var sex = jQuery('#userInfoSexInput').val();
          var surname = jQuery('#userInfoSurnameInput').val();
          var firstname = jQuery('#userInfoFirstnameInput').val();
          var currentLocation = jQuery('#userInfoCurrentLocationInput').val();
          var birthday = jQuery('#userInfoBirthdayInput').val();
          var height = jQuery('#userInfoHeightInput').val();
          var weight = jQuery('#userInfoWeightInput').val();
          var school = jQuery('#userInfoSchoolInput').val();
          var degree = jQuery('#userInfoDegreeInput').val();
          var userInfoData = {
            '_format':'json',
            'token':pie.user.data.access_token,
            'puid':pie.user.data.puid,
            'nickname':nickname,
            'surname':surname,
            'first_name':firstname,
            'sex':sex,
            'birthday':birthday,
            'current_city':currentLocation,
            'height':height,
            'weight':weight,
            'school':school,
            'degree':degree
          }
          jQuery.ajax({
            url:pie.serverBase+'/api/rest/users/'+pie.user.data.puid+'.json',
            type:'put',
            data:userInfoData,
            dataType:'json',
            success:function(reply){
              if(reply.status == 'success'){
                jQuery('#userInfo input').css('color','black');
                jQuery('#userInfo select').css('color','black');
                alert('更新成功');
              }
              else{
                alert(reply.code);
                for(var key in reply.message){
                  for(var iKey in reply.message[key]){
                    alert(reply.message[key][iKey])
                  }
                }
              }
            },
            error:function(err){
              alert('cuowu'+err);
            }
          })
        },
      },
      describe:{
        id:'userDescribe',
        changed:false,
        init:function(){
          jQuery('#userDescribePresentationInput').val(pie.user.info.presentation);
          jQuery('#userDescribeBirthPlaceInput').val(pie.user.info.birth_place.code);
          jQuery('#userDescribeOccupationInput').val(pie.user.info.occupation);
          jQuery('#userDescribeDomainInput').val(pie.user.info.domain);
        },
        show:function(){
          userWindow.details.curSonPage.hide();
          userWindow.details.curSonPage = this;
          jQuery('#'+this.id).fadeIn();
        },
        hide:function(){
          jQuery('#'+this.id).css('display','none');
        },
        change:function(item){
          jQuery('#'+item.id).css('color','red');
          jQuery('#userDetails .headerRight span').html('提交');
          jQuery('#userDetails .headerRight').click(function(){
            userWindow.details.describe.submit();
          })
          this.changed = true;
        },
        submit:function(){
          if(!this.changed){return;}
          var presentation = jQuery('#userDescribePresentationInput').val();
          var birthPlace = jQuery('#userDescribeBirthPlaceInput').val();
          var occupation = jQuery('#userDescribeOccupationInput').val();
          var domain = jQuery('#userDescribeDomainInput').val();
          var userInfoData = {
            '_format':'json',
            'token':pie.user.data.access_token,
            'puid':pie.user.data.puid,
            'presentation':presentation,
            'birth_place':birthPlace,
            'occupation':occupation,
            'domain':domain
          }
          jQuery.ajax({
            url:pie.serverBase+'/api/rest/users/'+pie.user.data.puid+'.json',
            type:'put',
            data:userInfoData,
            dataType:'json',
            success:function(reply){
              if(reply.status == 'success'){
                jQuery('#userDescribe textarea').css('color','black');
                jQuery('#userDescribe input').css('color','black');
                jQuery('#userDescribe select').css('color','black');
                alert('更新成功');
              }
              else{
                alert(reply.code);
                for(var key in reply.message){
                  for(var iKey in reply.message[key]){
                    alert(reply.message[key][iKey])
                  }
                }
              }
            },
            error:function(err){
              alert('cuowu'+err);
            }
          })
        },
      },
      expectation:{
        id:'userExpectation',
        changed:false,
        init:function(){
          jQuery('#userExpectationMinHeightInput').val(pie.user.expectations.height_min);
          jQuery('#userExpectationIdealHeightInput').val(pie.user.expectations.height_ideal);
          jQuery('#userExpectationMaxHeightInput').val(pie.user.expectations.height_max);
          jQuery('#userExpectationMinDegreeInput').val(pie.user.expectations.degree_min);
          jQuery('#userExpectationIdealDegreeInput').val(pie.user.expectations.degree_ideal);
          jQuery('#userExpectationMaxDegreeInput').val(pie.user.expectations.degree_max);
          jQuery('#userExpectationMinAgeInput').val(pie.user.expectations.age_diff_min);
          jQuery('#userExpectationIdealAgeInput').val(pie.user.expectations.age_diff_ideal);
          jQuery('#userExpectationMaxAgeInput').val(pie.user.expectations.age_diff_max);
          jQuery('#userExpectationIdealLocationInput').val(pie.user.expectations.location_ideal);
          jQuery('#userExpectationLocationDistanceInput').val(pie.user.expectations.location_distance);
        },
        show:function(){
          userWindow.details.curSonPage.hide();
          userWindow.details.curSonPage = this;
          jQuery('#'+this.id).fadeIn();
        },
        hide:function(){
          jQuery('#'+this.id).css('display','none');
        },
        change:function(item){
          jQuery('#'+item.id).css('color','red');
          jQuery('#userDetails .headerRight span').html('提交');
          jQuery('#userDetails .headerRight').click(function(){
            userWindow.details.expectation.submit();
          })
          this.changed = true;
        },
        submit:function(){
          if(!this.changed){return;}
          var height_min = jQuery('#userExpectationMinHeightInput').val();
          var height_ideal = jQuery('#userExpectationIdealHeightInput').val();
          var height_max = jQuery('#userExpectationMaxHeightInput').val();
          var degree_min = jQuery('#userExpectationMinDegreeInput').val();
          var degree_ideal = jQuery('#userExpectationIdealDegreeInput').val();
          var degree_max = jQuery('#userExpectationMaxDegreeInput').val();
          var age_diff_min = jQuery('#userExpectationMinAgeInput').val();
          var age_diff_ideal = jQuery('#userExpectationIdealAgeInput').val();
          var age_diff_max = jQuery('#userExpectationMaxAgeInput').val();
          var location_ideal = jQuery('#userExpectationIdealLocationInput').val();
          var location_distance = jQuery('#userExpectationLocationDistanceInput').val();
          var userInfoData = {
            'token':pie.user.data.access_token,
            'height_min':height_min,
            'height_ideal':height_ideal,
            'height_max':height_max,
            'degree_min':degree_min,
            'degree_ideal':degree_ideal,
            'degree_max':degree_max,
            'age_diff_min':age_diff_min,
            'age_diff_ideal':age_diff_ideal,
            'age_diff_max':age_diff_max,
            'location_ideal':location_ideal,
            'location_distance':location_distance
          }
          jQuery.ajax({
            url:pie.serverBase+'/api/rest/apps/pie_match/expectations.json',
            type:'put',
            data:userInfoData,
            dataType:'json',
            success:function(reply){
              if(reply.status == 'success'){
                jQuery('#userExpectation input').css('color','black');
                jQuery('#userExpectation select').css('color','black');
                alert('更新成功');
              }
              else{
                alert(reply.code);
                for(var key in reply.message){
                  for(var iKey in reply.message[key]){
                    alert(reply.message[key][iKey])
                  }
                }
              }
            },
            error:function(err){
              alert('cuowu'+err);
            }
          })
        }
      },
      resize:function(){},
      init:function(){
        this.curSonPage = this.info;
        this.curSonPage.show();
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    userTestIndexPage:{
      id:'userTestIndexPage',
      resize:function(){},
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    userSeeLoveTestPage:{
      id:'userSeeLoveTestPage',
      resize:function(){},
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
      init:function(n,x){
        return;
        if(x != undefined){
          if(x >= pieUser.seeLoveTest.length){
            alert('已经答完了该模块下的题')
            return;
          }
          var testData = {
            'token':pieUser.data.access_token,
            'answer':pieUser.seeLoveTest[x-1].possible_answers[n],
          }
          jQuery.ajax({
            url:serverBase+'/api/rest/apps/pie_test/questions/'
                +pieUser.seeLoveTest[x].code+'/my/answer.json',
            type:'post',
            dataType:'json',
            data:testData,
            success:function(reply){
              if(reply.status != 'success'){
                if(reply.code == 0){
                  alert('提交失败，原因是:该题已经提交过了')
                }
                return;
              }
              alert('提交成功');
            },
            error:function(err){
              alert('error:'+err);
            }
          })
        }
        else{
          x = 0;
        }
        jQuery('#'+this.id+' .userTestTitle').html(pieUser.seeLoveTest[x].question);
        var choices = pieUser.seeLoveTest[x].possible_answers;
        var choiceHtml = '';
        for(var i = 0; i < choices.length; i++){
          choiceHtml += "<li><a href='#'>"+choices[i]+"</a></li>";
        }
        jQuery('#'+this.id+' .userTestChoices').html(choiceHtml);
        pieAnimate('seeLove');
      },
    },
    userSeeLifeTestPage:{
      id:'userSeeLifeTestPage',
      resize:function(){},
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
      init:function(n,x){  //n表示上一题选中的题目选项,x表示下一题的题号
        return;
        if(x != undefined){
          if(x >= pie.user.seeLifeTest.length){
            alert('已经答完了该模块下的题')
            return;
          }
          var testData = {
            'token':pie.user.data.access_token,
            'answer':pie.user.seeLifeTest[x-1].possible_answers[n],
          }
          jQuery.ajax({
            url:pie.serverBase+'/api/rest/apps/pie_test/questions/'
            +pie.user.seeLifeTest[x].code+'/my/answer.json',
            type:'post',
            dataType:'json',
            data:testData,
            success:function(reply){
              if(reply.status != 'success'){
                if(reply.code == 0){
                  alert('提交失败，原因是:该题已经提交过了')
                }
                return;
              }
              alert('提交成功');
            },
            error:function(err){
              alert('error:'+err);
            }
          })
        }
        else{
          x = 0;
        }
        jQuery('#'+this.id+' .userTestTitle').html(pie.user.seeLifeTest[x].question);
        var choices = pie.user.seeLifeTest[x].possible_answers;
        var choiceHtml = '';
        for(var i = 0; i < choices.length; i++){
          choiceHtml += "<li><a href='#'>"+choices[i]+"</a></li>";
        }
        jQuery('#'+this.id+' .userTestChoices').html(choiceHtml);
        pie.ratingAnimate('seeLife');
      },
    },
    userPersonalityTestPage:{
      id:'userPersonalityTestPage',
      resize:function(){},
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
      init:function(n,x){  //n表示上一题选中的题目选项,x表示下一题的题号
        return;
        if(x != undefined){
          if(x >= pie.user.personalityTest.length){
            alert('已经答完了该模块下的题')
            return;
          }
          var testData = {
            'token':pie.user.data.access_token,
            'answer':pie.user.personalityTest[x-1].possible_answers[n],
          }
          jQuery.ajax({
            url:pie.serverBase+'/api/rest/apps/pie_test/questions/'
            +pie.user.personalityTest[x].code+'/my/answer.json',
            type:'post',
            dataType:'json',
            data:testData,
            success:function(reply){
              if(reply.status != 'success'){
                if(reply.code == 0){
                  alert('提交失败，原因是:该题已经提交过了')
                }
                return;
              }
              alert('提交成功');
            },
            error:function(err){
              alert('error:'+err);
            }
          })
        }
        else{
          x = 0;
        }
        jQuery('#'+this.id+' .userTestTitle').html(pie.user.personalityTest[x].question);
        var choices = pie.user.personalityTest[x].possible_answers;
        var choiceHtml = '';
        for(var i = 0; i < choices.length; i++){
          choiceHtml += "<li><a href='#'>"+choices[i]+"</a></li>";
        }
        jQuery('#'+this.id+' .userTestChoices').html(choiceHtml);
        pie.ratingAnimate('personality');
      },
    },
    userCommunicationTestPage:{
      id:'userCommunicationTestPage',
      resize:function(){},
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
      init:function(n,x){  //n表示上一题选中的题目选项,x表示下一题的题号
        return;
        if(x != undefined){
          if(x >= pie.user.communicationTest.length){
            alert('已经答完了该模块下的题')
            return;
          }
          var testData = {
            'token':pie.user.data.access_token,
            'answer':pie.user.communicationTest[x-1].possible_answers[n],
          }
          jQuery.ajax({
            url:pie.serverBase+'/api/rest/apps/pie_test/questions/'
            +pie.user.communicationTest[x].code+'/my/answer.json',
            type:'post',
            dataType:'json',
            data:testData,
            success:function(reply){
              if(reply.status != 'success'){
                if(reply.code == 0){
                  alert('提交失败，原因是:该题已经提交过了')
                }
                return;
              }
              alert('提交成功');
            },
            error:function(err){
              alert('error:'+err);
            }
          })
        }
        else{
          x = 0;
        }
        jQuery('#'+this.id+' .userTestTitle').html(pie.user.communicationTest[x].question);
        var choices = pie.user.communicationTest[x].possible_answers;
        var choiceHtml = '';
        for(var i = 0; i < choices.length; i++){
          choiceHtml += "<li><a href='#'>"+choices[i]+"</a></li>";
        }
        jQuery('#'+this.id+' .userTestChoices').html(choiceHtml);
        pie.ratingAnimate('communication');
      },
    },
    userQAPage:{
      id:'userQAPage',
      resize:function(){},
      init:function(x){
        if(x != undefined){
          pie.user.questionStatus[x] = 1;
          var qData = {
            'token':pie.user.data.access_token,
            'answer':pie.user.questions[x].possible_answers[0],
          }
          jQuery.ajax({
            url:pie.serverBase+'/api/rest/apps/pie_match/qa/public/questions/'
            +pie.user.questions[x].code+'/my/answers.json',
            type:'post',
            dataType:'json',
            data:qData,
            success:function(reply){
            },
            error:function(err){
              alert('提交问题'+pie.user.questions[x].code+'出错'+err);
            },
          })
        }
        var questionRandom = -1;
        for(var i = 0; i < pie.user.questionStatus.length; i++){
          if(pie.user.questionStatus[i] == 0){
            questionRandom = i;
            break;
          }
        }
        if(questionRandom < 0){
          pie.user.getQuestions();
          return;
        }
        var choices = pie.user.questions[questionRandom].possible_answers;
        var choiceHtml = "";
        for(var i = 0; i < choices.length; i++){
          choiceHtml += "<div class='pieQuestionChoice' onclick='userWindow.questions.init("
            +questionRandom+")'>"+choices[i]+"</div>";
        }
        jQuery('#pieQuestionTitle').html(pie.user.questions[questionRandom].title);
        jQuery('#pieQuestionChoices').html(choiceHtml);
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    userMessagePage:{
      id:'userMessagePage',
      resize:function(){},
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    userSettingPage:{
      id:'userSettingPage',
      resize:function(){},
      init:function(){

      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    mateHomePage:{
      id:'mateHomePage',
      resize:function(){},
      init:function(){
        if(pie.mate.info.photos.length > 0){
          jQuery('#mateHomeHead img').attr('src',pie.uploadBase+'/uploads/attachments/'+pie.mate.info.photos[0]);
        }
        jQuery('#mateName').html(pie.mate.info.nickname);
        jQuery('#mateSign').html(pie.mate.info.presentation);
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    mateDetailPage:{
      id:'mateDetailPage',
      resize:function(){},
      init:function(){
        jQuery('#mateInfoNicknameInput').html(pie.mate.info.nickname);
        if(pie.mate.info.sex == true){
          jQuery('#mateInfoSexInput').html('男');
        }
        else{
          jQuery('#mateInfoSexInput').html('女');
        }
        jQuery('#mateInfoCurrentLocationInput').html(pie.mate.info.current_location.name);
        jQuery('#mateInfoBirthdayInput').html(pie.mate.info.birthday);
        jQuery('#mateInfoHeightInput').html(pie.mate.info.height);
        jQuery('#mateInfoWeightInput').html(pie.mate.info.weight);
        jQuery('#mateInfoSchoolInput').html(pie.mate.info.school);
        jQuery('#mateInfoDegreeInput').html(pie.mate.info.degree);
        jQuery('#mateInfoPresentationInput').html(pie.mate.info.presentation);
        jQuery('#mateInfoBirthPlaceInput').html(pie.mate.info.birth_place.name);
        jQuery('#mateInfoOccupationInput').html(pie.mate.info.occupation);
        jQuery('#mateInfoDomainInput').html(pie.mate.info.domain);
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    mateTestPage:{
      id:'mateTestPage',
      curSonPage:null,
      index:{
        id:'mateTestIndexSonPage',
        init:function(){
        },
        show:function(){
          mateWindow.tests.curSonPage.hide();
          mateWindow.tests.curSonPage = this;
          jQuery('#'+this.id).css('display','block');
          jQuery('#mateTests .headerLeft span').html('');
        },
        hide:function(){
          jQuery('#'+this.id).css('display','none');
        },
      },
      seeLove:{
        id:'mateSeeLoveTestSonPage',
        show:function(){
          mateWindow.tests.curSonPage.hide();
          mateWindow.tests.curSonPage = this;
          jQuery('#'+this.id).css('display','block');
          jQuery('#mateTests .headerLeft span').html('返回');
          jQuery('#mateTests .headerLeft').click(function(){
            mateWindow.tests.index.show();
          });
        },
        hide:function(){
          jQuery('#'+this.id).css('display','none');
        },
        init:function(n,x){  //n表示上一题选中的题目选项,x表示下一题的题号
        },
      },
      seeLife:{
        id:'userSeeLifeTestSonPage',
        show:function(){
          mateWindow.tests.curSonPage.hide();
          mateWindow.tests.curSonPage = this;
          jQuery('#'+this.id).css('display','block');
          jQuery('#mateTests .headerLeft span').html('返回');
          jQuery('#mateTests .headerLeft').click(function(){
            mateWindow.tests.index.show();
          });
        },
        hide:function(){
          jQuery('#'+this.id).css('display','none');
        },
        init:function(n,x){  //n表示上一题选中的题目选项,x表示下一题的题号
        },
      },
      personality:{
        id:'userPersonalityTestSonPage',
        show:function(){
          mateWindow.tests.curSonPage.hide();
          mateWindow.tests.curSonPage = this;
          jQuery('#'+this.id).css('display','block');
          jQuery('#mateTests .headerLeft span').html('返回');
          jQuery('#mateTests .headerLeft').click(function(){
            mateWindow.tests.index.show();
          });
        },
        hide:function(){
          jQuery('#'+this.id).css('display','none');
        },
        init:function(n,x){  //n表示上一题选中的题目选项,x表示下一题的题号
        },
      },
      communication:{
        id:'userCommunicationTestSonPage',
        show:function(){
          mateWindow.tests.curSonPage.hide();
          mateWindow.tests.curSonPage = this;
          jQuery('#'+this.id).css('display','block');
          jQuery('#mateTests .headerLeft span').html('返回');
          jQuery('#mateTests .headerLeft').click(function(){
            mateWindow.tests.index.show();
          });
        },
        hide:function(){
          jQuery('#'+this.id).css('display','none');
        },
        init:function(n,x){  //n表示上一题选中的题目选项,x表示下一题的题号
        },
      },
      resize:function(){},
      init:function(){
        this.curSonPage = this.index;
        this.curSonPage.show();
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    mateQAPage:{
      id:'mateQAPage',
      resize:function(){},
      init:function(){
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    matePhotoPage:{
      id:'matePhotoPage',
      curSonPageNum:0,
      maxSonPageNum:null,
      resize:function(){},
      init:function(){
        jQuery('#matePhotos .innerPageContent').html('');
        var photoHtml = '';
        for(var i = 0; i < pie.mate.info.photos.length; i++){
          photoHtml += '<img class="matePhoto" src="'+pie.uploadBase+'/uploads/attachments/'+pie.mate.info.photos[i]+'"/>';
          if((i+1)%10==0){
            jQuery('#matePhotos .innerPageContent').append("<div class='sonPage' id='matePhotoSonPage"+Math.floor(i/10)
              +"'></div>");
            jQuery('#matePhotoSonPage'+Math.floor(i/10)).html(photoHtml);
            photoHtml = '';
          }
        }
        if(photoHtml != ''){
          jQuery('#matePhotos .innerPageContent').append("<div class='sonPage' id='matePhotoSonPage"+Math.floor(pie.mate.info.photos.length/10)
            +"'></div>");
          jQuery('#matePhotoSonPage'+Math.floor(pie.mate.info.photos.length/10)).html(photoHtml);
        }
        this.maxSonPageNum = Math.floor((pie.mate.info.photos.length-1)/10);
        jQuery('#matePhotoSonPage'+this.curSonPageNum).css('display','block');
        jQuery('#matePhotos .headerRight span').html('');
        jQuery('#matePhotos .headerRight').unbind();
        jQuery('#matePhotos .headerLeft span').html('');
        jQuery('#matePhotos .headerLeft').unbind();
        if(this.curSonPageNum < this.maxSonPageNum){
          jQuery('#matePhotos .headerRight span').html('下一页');
          jQuery('#matePhotos .headerRight').click(function(){
            mateWindow.photos.showPage(mateWindow.photos.curSonPageNum+1);
          });
        }
        if(this.curSonPageNum > 0){
          jQuery('#matePhotos .headerLeft span').html('上一页');
          jQuery('#matePhotos .headerLeft').click(function(){
            mateWindow.photos.showPage(mateWindow.photos.curSonPageNum-1);
          });
        }
      },
      showPage:function(n){
        if(this.curSonPageNum == n){return;}
        jQuery('#matePhotoSonPage'+this.curSonPageNum).css('display','none');
        this.curSonPageNum = n;
        jQuery('#matePhotoSonPage'+this.curSonPageNum).css('display','block');
        jQuery('#matePhotos .headerRight span').html('');
        jQuery('#matePhotos .headerRight').unbind();
        jQuery('#matePhotos .headerLeft span').html('');
        jQuery('#matePhotos .headerLeft').unbind();
        if(this.curSonPageNum < this.maxSonPageNum){
          jQuery('#matePhotos .headerRight span').html('下一页');
          jQuery('#matePhotos .headerRight').click(function(){
            mateWindow.photos.showPage(mateWindow.photos.curSonPageNum+1);
          });
        }
        if(this.curSonPageNum > 0){
          jQuery('#matePhotos .headerLeft span').html('上一页');
          jQuery('#matePhotos .headerLeft').click(function(){
            mateWindow.photos.showPage(mateWindow.photos.curSonPageNum-1);
          });
        }
      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
    mateTalkPage:{
      id:'mateTalkPage',
      resize:function(){},
      init:function(){

      },
      show:function(){
        if(pie.curPageId != undefined){
          pie.page[pie.curPageId].hide();
        }
        pie.curPageId = this.id;
        jQuery('#'+this.id).css('display','block');
        this.getFocus();
        jQuery('#'+this.id+'Header').css('display','block');
        jQuery('#'+this.id+'Footer').css('display','block');
      },
      hide:function(){
        jQuery('#'+this.id).css('display','none');
        jQuery('#'+this.id+'Header').css('display','none');
        jQuery('#'+this.id+'Footer').css('display','none');
      },
      getFocus:function(){},
    },
  },
  resize:function(){
    this.height = window.innerHeight;
    this.width = Math.min(400,window.innerWidth);
    var mLeft = (window.innerWidth-this.width)/2;
    jQuery('.pieHeader').css('width',this.width);
    jQuery('.pieFooter').css('width',this.width);
    jQuery('#pie').css('width',this.width);
    jQuery('#pie').css('margin-left',mLeft);
    jQuery('.pageNav').css('width',Math.floor(this.width/3)+'px');
    jQuery('.pageNav').css('height',Math.floor(this.width/3)+'px');
    for(var key in this.page){
      this.page[key].resize();
    }
  },
  init:function(){
    this.resize();
    jQuery('#indexPage').fadeIn(1000,function(){
      jQuery('#indexPage').fadeOut(1000,function(){
        if(window.localStorage.pieUserAccount&&window.localStorage.pieUserPassword){
          /*delete window.localStorage.pieUserAccount;
          delete window.localStorage.pieUserPassword;*/
          jQuery('#pieLoginAccountInput').val(window.localStorage.pieUserAccount);
          jQuery('#pieLoginPasswordInput').val(window.localStorage.pieUserPassword);
          pie.page.pieLoginPage.submit();
        }
        else{
          pie.page.pieLoginPage.show();
        }
      });
    })
  },
  ratingAnimate:function(id){
    var animationTime = 500;
    // Variable to store the colours
    var colours = ["bd2c33", "e49420", "ecdb00", "3bad54", "1b7db9"];
    // Add rating information box after rating
    var ratingInfobox = $("<div />")
      .attr("id", id+"ratinginfo")
      .attr("class","ratingInfo")
      .insertAfter($("#"+id+"rating"));
    // Function to colorize the right ratings
    var colourizeRatings = function(nrOfRatings) {
      $("#"+id+"rating li a").each(function() {
        if($(this).parent().index() <= nrOfRatings) {
          $(this).stop().animate({ backgroundColor : "#" + colours[nrOfRatings] } , animationTime);
        }
      });
    };
    // Handle the hover events
    $("#"+id+"rating li a").hover(function() {
      // Empty the rating info box and fade in
      ratingInfobox
        .empty()
        .stop()
        .animate({ opacity : 1 }, animationTime);
      // Add the text to the rating info box
      $("<p />")
        .html($(this).html())
        .appendTo(ratingInfobox);
      // Call the colourize function with the given index
      colourizeRatings($(this).parent().index());
    }, function() {
      // Fade out the rating information box
      ratingInfobox
        .stop()
        .animate({ opacity : 0 }, animationTime);
      // Restore all the rating to their original colours
      $("#"+id+"rating li a").stop().animate({ backgroundColor : "#fcb0cd" } , animationTime);
    });
    // Prevent the click event and show the rating
    $("#"+id+"rating li a").click(function(e) {
      e.preventDefault();
      alert("You voted on item number " + ($(this).parent().index() + 1));
    });
  },
}
/*<iframe src="{{ oauth_login_pages['renren'] }}">
</iframe>*/